package org.jbp.timelapse;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;

public class TimeLapseActivity extends Activity
{
	private EditText etNShots;
	private EditText etDelay;
	private Button bStart;
	private ProgressBar pbTotal;
	private LogTextView logView;
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.main);
	}
	
	public void start(View view)
	{
		try
		{
			getTextNShots().setEnabled(false);
			getTextDelay().setEnabled(false);
			getButtonStart().setEnabled(false);
			
			String sShots = getTextNShots().getText().toString().trim();
			String sDelay = getTextDelay().getText().toString().trim();
			
			Integer nShots = Integer.valueOf(sShots);
			Integer delay = Integer.valueOf(sDelay);
			
			getLogView().setText("");
			getLogView().info("Taking "+nShots+" photos in "+delay+"ms intervals.");
			
			TakePictureTask takePictureTask = new TakePictureTask(getLogView(), getProgressBar());
			takePictureTask.execute(new Integer[]{nShots, delay});
		}
		catch(NumberFormatException e)
		{
			getLogView().error(e.getMessage());
		}
		finally
		{
			//TODO ker je klic asinhron, se to takoj spet nastavi na enabled.
			getTextNShots().setEnabled(true);
			getTextDelay().setEnabled(true);
			getButtonStart().setEnabled(true);
		}
	}
	
	private EditText getTextNShots()
	{
		if(etNShots == null)
		{
			etNShots = (EditText)findViewById(R.id.editTextNShots);
		}
		return etNShots;
	}
	
	private EditText getTextDelay()
	{
		if(etDelay == null)
		{
			etDelay = (EditText)findViewById(R.id.editTextDelay);
		}
		return etDelay;
	}
	
	private Button getButtonStart()
	{
		if(bStart == null)
		{
			bStart = (Button)findViewById(R.id.buttonStart);
		}
		return bStart;
	}
	
	private ProgressBar getProgressBar()
	{
		if(pbTotal == null)
		{
			pbTotal = (ProgressBar)findViewById(R.id.progressBarTotal);
		}
		return pbTotal;
	}
	
	private LogTextView getLogView()
	{
		if(logView == null)
		{
			logView = (LogTextView)findViewById(R.id.LogTextView);
		}
		return logView;
	}
}